package com.mikey.controller;

import com.mikey.common.ApiResult;
import com.mikey.pojo.Wish;
import com.mikey.pojo.WishFormat;
import com.mikey.service.WishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author :Mikey
 * @description :
 * @create :2022-12-05 11:48:00
 */
@RestController
public class WishController {
    @Autowired
    private WishService wishService;

    @GetMapping("/getInit")
    public ApiResult getList() {
        List<Wish> wishes = wishService.selectAll();
        List<WishFormat> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        BeanUtils.copyProperties(wishes, list);
        for (int i = 0; i < wishes.size(); i++) {
            WishFormat wishFormat = new WishFormat();
            BeanUtils.copyProperties(wishes.get(i), wishFormat);
            wishFormat.setTime(sdf.format(wishes.get(i).getTime()));
            list.add(wishFormat);
        }
        return ApiResult.success(10000, "success", list);
    }

    @PostMapping("/getByName")
    public ApiResult getListByName(@RequestParam String name) {
        List<Wish> wishes = wishService.selectByName(name);
        List<WishFormat> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        BeanUtils.copyProperties(wishes, list);
        for (int i = 0; i < wishes.size(); i++) {
            WishFormat wishFormat = new WishFormat();
            BeanUtils.copyProperties(wishes.get(i), wishFormat);
            wishFormat.setTime(sdf.format(wishes.get(i).getTime()));
            list.add(wishFormat);
        }
        return ApiResult.success(10000, "success", list);
    }

    @PostMapping("/insert")
    public ApiResult getListByName(String text, String name) {
        int id = 0;
        Data data = null;
        Wish wish = new Wish(id, name, text, (Date) data);
        wish.setName(name);
        wish.setText(text);
        return ApiResult.success(10000, "success", wishService.insertWish(wish));
    }
}
